package liurong.driver.sync;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class SyncFutureTest {
	private final static Map<Integer, CustomSyncUtil<String>> map = new ConcurrentHashMap<Integer, CustomSyncUtil<String>>();

	public static void main(String[] args) throws Exception {
		new Thread() {
			@Override
			public void run() {
				CustomSyncUtil<String> syncFuture = new CustomSyncUtil<String>();
				map.put(1, syncFuture);
				try {
					String value = syncFuture.getResponse(1, TimeUnit.SECONDS);
					System.out.println(value);
				} catch (Exception e) {
					map.remove(1);
					e.printStackTrace();
				}
			}
		}.start();

		TimeUnit.SECONDS.sleep(10);

		CustomSyncUtil<String> syncFuture = map.remove(1);
		syncFuture.setRespose("我的第一个同步调用");
	}
	// public static void main(String[] args) throws Exception {
	// new Thread() {
	// @Override
	// public void run() {
	// SyncFuture<String> syncFuture = new SyncFuture<String>();
	// map.put(1, syncFuture);
	// try {
	// String value = syncFuture.get(2, TimeUnit.SECONDS);
	// System.out.println(value);
	// } catch (InterruptedException e) {
	// map.remove(1);
	// e.printStackTrace();
	// }
	// }
	// }.start();
	//
	// TimeUnit.SECONDS.sleep(1);
	//
	// SyncFuture<String> syncFuture = map.remove(1);
	// syncFuture.setResponse("我的第一个同步调用");
	// }
}
